package com.example.controller;

import com.example.common.enums.ResponseCodeEnum;
import com.example.common.utils.ApiResult;
import com.example.model.entity.Supplies;
import com.example.model.query.SuppliesQuery;
import com.example.model.vo.SuppliesVo;
import com.example.service.SuppliesService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Supplies Controller
 * 2020-08-27 18:59
 */
@Controller
@RestController
@RequestMapping("/supplies")
@CrossOrigin
@Api(value = "物资管理",tags={"物资管理接口"})
public class SuppliesController {

	private static final Logger logger = LoggerFactory.getLogger(SuppliesController.class);
	private int pageSize = 10;
	private int currentPage = 1;

	@Value("${sys_error:}")
	private String sysError;

	@Value("${login_timeout:}")
	private String loginTimeout;

	@Autowired
	private SuppliesService suppliesService;

	/**
	 * 分页查询列表
	 */
	@GetMapping("/page")
	public ApiResult<SuppliesQuery> pageByQuery(SuppliesQuery query) {
		try {
			return suppliesService.pageUserVoByQuery(query);
		}   catch (Exception e) {
			e.printStackTrace();
			return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_PAGE_AD_FALSE);
		}
	}

	@PostMapping("/save")
	public ApiResult<Boolean> save(@RequestBody SuppliesVo suppliesVo) {
		try {
			return suppliesService.saveAd(suppliesVo);
		}  catch (Exception e) {
			e.printStackTrace();
			return new ApiResult<>(Boolean.FALSE, ResponseCodeEnum.MALL_SERVICE_SAVE_AD_FALSE);
		}
	}

	@GetMapping("/get/{id}")
	public ApiResult<SuppliesVo> get(@PathVariable("id") String id){
		try {
			return suppliesService.getInfo(id);
		}catch (Exception e){
			e.printStackTrace();
			return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_ORDER_GET_FALSE);
		}
	}

	@PostMapping("/modify")
	public ApiResult<Boolean> update(@RequestBody SuppliesVo suppliesVo) {
		try {
			return suppliesService.update(suppliesVo);
		}catch (Exception e){
			e.printStackTrace();
			return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_ORDER_UPDATE_FALSE);
		}
	}

	@GetMapping("/del/{id}")
	public ApiResult<Boolean> del(@PathVariable("id") String id){
		try {
			return suppliesService.del(id);
		}catch (Exception e){
			e.printStackTrace();
			return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_ORDER_GET_FALSE);
		}
	}

	/**
	 * 物资下拉框
	 */
	@GetMapping("/getList")
	public ApiResult<List<Supplies>> getList() {
		try {
			return suppliesService.getList();
		}   catch (Exception e) {
			e.printStackTrace();
			return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_PAGE_AD_FALSE);
		}
	}
}
